package com.house.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.house.dao.LeaseMapper;
import com.house.entity.House;
import com.house.entity.Lease;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;


/**
 * 租赁信息模块服务层
 */
@Service
@Slf4j
public class LeaseService extends ServiceImpl<LeaseMapper, Lease> {


    @Autowired
    HouseService houseService;



    /**
     * 分页查询租赁信息 （带房屋信息）
     * @param lease
     * @return
     */
    public List<Lease> findByPage(Lease lease) {
        lease.setPageNumber((lease.getPageNumber() - 1) * lease.getPageSize());

        log.info("分页查询租赁信息 pageNumber: {}, pageSize: {}", lease.getPageNumber(), lease.getPageSize());

        List<Lease> leases = baseMapper.findByPage(lease);

        // 遍历填充房屋图片
        for(Lease l : leases) {
            houseService.imageProcessor(l.getHouse());
        }
        return leases;
    }


    /**
     * 获取有效租赁信息总数量
     * @return
     */
    public int getValidCount(Lease lease) {
        return baseMapper.count(lease);
    }


    /**
     * 生成测试数据
     */
    public void createTestData() {
        List<House> houses = houseService.list();
        Random rand = new Random();

        for(int i = 1; i<= 10; i++) {
            Lease lease = new Lease();
            lease.init();
            lease.setStatus("1");
            lease.setTitle(
                HouseService.xiaoqu.get(rand.nextInt(HouseService.xiaoqu.size())) + " " +
                HouseService.biaoqian.get(rand.nextInt(HouseService.biaoqian.size())));
            lease.setPrice(String.valueOf((rand.nextInt(50) + 1) * 100));
            lease.setMinNumber(String.valueOf(rand.nextInt(11) + 1));
            lease.setHouseId(houses.get(rand.nextInt(houses.size())).getId());
            lease.setBusiName(HouseService.name.get(rand.nextInt(HouseService.name.size())));
            lease.setBusiMobile(String.valueOf(rand.nextInt(999999999) + 18000000000L));
            lease.setRemark(HouseService.biaoqian.get(rand.nextInt(HouseService.biaoqian.size())));
            super.saveOrUpdate(lease);
        }

    }
}
